trigger UpdateAttachmentParent on Attachment (after insert) {
    List<ID> NewIdList = new List<ID>();
    List<ID> ParentIdList = new List<ID>();
    List<VisitAndOrderInProcess__c> VisitList = new List<VisitAndOrderInProcess__c>();
        
    List<Attachment> AttList = new List<Attachment>();
    List<Attachment> UAttList = new List<Attachment>();
            
    if(system.trigger.isinsert){
        for(Attachment att : Trigger.new){
            NewIdList.add(att.id);
            ParentIdList.add(att.ParentId);
        }
        AttList = [Select id,Name,ParentId From Attachment Where id in: NewIdList];
        VisitList = [Select id,Type__c,Order__c,InStoreVisit__c From VisitAndOrderInProcess__c];                
        for(Attachment a : AttList)
        {
            for(VisitAndOrderInProcess__c v: VisitList)
            {
                if(a.ParentId == v.id)
                {
                    if(v.Type__c == 'In Store Visit'){
                        a.ParentId = v.InStoreVisit__c;
                        UAttList.add(a);
                    }
                    if(v.Type__c == 'Order'){
                        a.ParentId = v.Order__c;
                        UAttList.add(a);
                    }                   
                }
            }
        }
        update UAttList;
    }
}